var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        form: {
            startMonth: '',
            MeterCategoryID: 0,
            MeterCategoryIDs: [],
            MeterStatusID: 0,
            keywords: '',
            pageindex: 0,
            pagesize: 10,
        },
        minDate: new Date(),
        maxDate: new Date(),
        currentDate: 0,
        ProjectID: 0,
        ProjectIDs: '',
        RoomIDs: '',
        FullName: '',
        RoomList: [],
        searchbar_click: false,
        current_item_length: 0,
        scroll_top: 0,
        can_scroll: true,
        is_searching: false,
        headerlist: [],
        MeterList: [
            { ID: 0, Name: '全部' },
            { ID: 2, Name: '电表' },
            { ID: 1, Name: '水表' },
            { ID: 3, Name: '气表' },
        ],
        IsAllChecked: false,
        chooseList: [],
        MeterReadType: 1,//1-房间表 2-公表 3-总表
        MeterStatusList: [
            { ID: 0, Name: '全部' },
            { ID: 1, Name: '本月已抄' },
            { ID: 2, Name: '本月未抄' },
        ],
        topHeight: 134,
    },
    methods: {
        doReset: function (type) {
            var that = this;
            if (type == 2) {
            } else {
                that.form.MeterStatusID = 0;
                that.form.MeterCategoryIDs = [];
            }
        },
        doSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.searchbar_click = false;
            that.do_search();
            ns.init_search();
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //抄表日期
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;

            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2) { //按月 用的van-popup 自带了遮罩
                that.showsearch = false;
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
            that.showPopover = false;
        },
        onConfirm(day) {
            var that = this;
            var nowdate = moment(day).format('YYYY-MM-DD');
            that.form.startMonth = nowdate;
            that.do_search();
        },
        do_search: function () {
            var that = this;
            that.searchbar_click = true;
            that.doHideSearch();
            that.do_reload();
        },
        do_reload: function () {
            var that = this;
            that.form.pageindex = 0;
            that.get_data();
        },
        doSelectMeterCategory: function (ID, Name) {
            // var that = this;
            // that.form.MeterCategoryID = ID;
            // that.form.MeterCategoryIDs = [];
            // that.form.MeterCategoryIDs.push(ID);
            // that.showsearchparam = false;
            // that.do_search();
            var that = this;
            var index = that.form.MeterCategoryIDs.indexOf(ID);
            if (index > -1) {
                that.form.MeterCategoryIDs.splice(index, 1);
            } else {
                that.form.MeterCategoryIDs.push(ID);
            }
        },
        doSelectMeterStatus: function (ID, Name) {
            var that = this;
            that.form.MeterStatusID = ID;
            // that.showsearchparam = false;
            // that.do_search();
        },
        get_data: function () {
            var that = this;
            // if (that.searchbar_click) {
            //     that.searchbar_click = false;
            //     if (!that.form.keywords) {
            //         api.toast({
            //             msg: '请输入关键字',
            //             duration: 2000,
            //             location: 'bottom'
            //         });
            //         return;
            //     }
            // }
            that.RoomList = [];
            that.chooseList = [];
            that.IsAllChecked = false;
            // that.is_searching = true;
            // that.can_scroll = false;
            var options = {};
            options.action = "getchargemeterroomlist";
            options.ProjectID = that.ProjectID;
            options.keywords = that.form.keywords;
            options.WriteStatus = that.form.MeterStatusID;
            options.Type = that.MeterReadType;
            let meterTypeIDs = that.form.MeterCategoryIDs;
            var index = meterTypeIDs.indexOf(0);
            if (index > -1) {
                meterTypeIDs = [];
            }
            options.MeterCategoryIDs = JSON.stringify(meterTypeIDs);
            options.WriteDate = that.form.startMonth;
            // console.log(JSON.stringify(options));
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {
                toast.hide();
                if (succeed) {
                    that.RoomList = data.data;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_select_project: function () {
            var that = this;
            ns.openWin('chooseproject_frm', '选择项目', {
                selecttype: 1
            });
        },
        get_select_project: function (data) {
            var that = this;
            that.ProjectID = data.roomid ? data.roomid : data.projectid;//末级ID
            that.ProjectIDs = data.projectids;
            that.RoomIDs = data.roomids;
            that.FullName = data.name;
            that.headerlist = eval('(' + data.headerlist + ')')
            setTimeout(function () {
                that.do_reload();
                var top_h = $api.offset($api.byId('topheadercontent')).h
                $api.css($api.byId('topheader'), 'height:' + top_h + 'px');
                $api.css($api.byId('main_refresh'), 'margin-top:' + top_h + 'px');
                that.topHeight = top_h;
            }, 500);
        },
        init_FullName: function () {
            var that = this;
            if (that.FullName == '') {
                that.FullName = '请选择资源信息';
            }
        },
        do_open_header: function (item) {
            var that = this;
            if (!item.isParent) {
                return;
            }
            ns.openWin('chooseproject_frm', '选择项目', {
                parentid: item.id,
                selecttype: 1
            });
        },
        //点击可勾选/取消勾选全部已返回的仪表信息
        showChooseAll() {
            var that = this;
            that.chooseList = [];
            //房间表
            if (that.MeterReadType == 1) {
                //取消全选
                if (that.IsAllChecked) {
                    that.RoomList.forEach((item, index, array) => {
                        if (item.List.length > 0) {
                            item.List.forEach((items, indexs, arrays) => {
                                items.IsSelect = false;
                            });
                        }
                    });
                }
                //全选
                else {
                    that.RoomList.forEach((item, index, array) => {
                        if (item.List.length > 0) {
                            item.List.forEach((items, indexs, arrays) => {
                                items.IsSelect = true;
                                that.chooseList.push(items);
                                // //判断断是否本月已抄
                                // if (!items.IsMonthWrite) {
                                //     items.IsSelect = true;
                                //     that.chooseList.push(items);
                                // }
                            });
                        }
                    });
                }
            }
            //公共表、总表
            else {
                //取消全选
                if (that.IsAllChecked) {
                    that.RoomList.forEach((item, index, array) => {
                        item.IsSelect = false;
                    });
                }
                //全选
                else {
                    that.RoomList.forEach((item, index, array) => {
                        item.IsSelect = true;
                        that.chooseList.push(item);
                        // //判断断是否本月已抄
                        // if (!item.IsMonthWrite) {
                        //     item.IsSelect = true;
                        //     that.chooseList.push(item);
                        // }
                    });
                }
            }

        },
        onSelectItem(row) {
            var that = this;
            if (row.IsMeterRead) {
                return;
            }
            row.IsSelect = !row.IsSelect;
            let findIndex = that.chooseList.findIndex((item) => {
                return item.ID == row.ID && item.RoomID == row.RoomID;
            });
            if (row.IsSelect) {
                that.IsAllChecked = true;
                //不存在-添加
                if (findIndex < 0) {
                    that.chooseList.push(row);
                }
            } else {
                //存在-删除
                if (findIndex > -1) {
                    that.chooseList.splice(findIndex, 1);
                }
                if (that.chooseList.length <= 0) {
                    that.IsAllChecked = false;
                }
            }
        },
        onOpenRecord(item) {
            var that = this;
            var IDs = [];
            if (that.MeterReadType == 1 && item.List.length > 0) {
                item.List.forEach((items, indexs, arrays) => {
                    IDs.push(items.ID);
                });
            }
            else {
                IDs.push(item.ID);
            }
            ns.openWin('meterwriterecord_more', '抄表记录', {
                ProjectID: item.ProjectID,
                MeterReadType: that.MeterReadType,
                IDs: JSON.stringify(IDs),
            });
        },
        //录入读数
        do_save() {
            var that = this;
            if (that.chooseList.length <= 0) {
                return;
            }
            //接口判断
            let IDs = [];
            that.chooseList.forEach((item, index, array) => {
                IDs.push(item.ID);
            });
            var options = {};
            options.action = "getlastmeterroompointlist";
            options.Ids = JSON.stringify(IDs);
            options.writeDate = that.form.startMonth;
            options.type = that.MeterReadType;
            // console.log(JSON.stringify(options));
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                toast.hide();
                if (succeed) {
                    ns.openWin('meterwriteread_more', '抄表读数', {
                        startMonth: that.form.startMonth,
                        chooseList: that.chooseList,
                        MeterType: that.MeterReadType,
                    });
                    //是否根据数据重新赋值读数
                    // if(data.data.length>0){
                    //     api.alert({
                    //         title: '提示信息',
                    //         msg: '当前选择仪表的抄表日期后，已存在抄表记录。请重新选择仪表或抄表日期。',
                    //     }, function () {
                    //     });
                    //     return;
                    // }
                    // else{
                    //     ns.openWin('meterwriteread_more', '抄表读数', {
                    //         startMonth: that.form.startMonth,
                    //         chooseList: that.chooseList
                    //     });
                    // }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 5000,
                        location: 'bottom'
                    });
                    return;
                }
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;

    var metertime = api.pageParam.time || '';//时间
    app.MeterReadType = api.pageParam.type || 1;//表类型
    app.ProjectID = api.pageParam.projectId || 0;
    toast = new auiToast();
    if (app.MeterReadType == 1) {
        app.get_select_project(api.pageParam);//选择资源
    }
    else {
        setTimeout(function () {
            app.get_data();
        }, 300);
    }

    app.minDate = new Date(moment(new Date()).add(-20, 'y'));
    app.maxDate = new Date(moment(new Date()).add(50, 'y'));
    app.currentDate = new Date();
    var nowdate = moment(app.currentDate).format('YYYY-MM');
    if (metertime != undefined && metertime != '') {
        var nowmonth=new Date(nowdate).getMonth();
        var metertimemonth=new Date(metertime).getMonth();
        if(nowmonth==metertimemonth){
            app.form.startMonth = moment(app.currentDate).format('YYYY-MM-DD');
        }
        else{
            app.form.startMonth = metertime + '-01';
        } 
    }
    else {
        app.form.startMonth = nowdate + '-01';
    }
    app.currentDate = new Date(app.form.startMonth);

    app.doSelectMeterCategory(app.MeterList[0].ID, app.MeterList[0].Name);
    app.doSelectMeterStatus(app.MeterStatusList[0].ID, app.MeterStatusList[0].Name);

    api.addEventListener({
        name: 'do_choose_project_complete'
    }, function (ret) {
        app.get_select_project(ret.value);
    })
    api.addEventListener({
        name: 'reload_chhoose_read'
    }, function (ret) {
        app.get_data();
    })

    app.init_FullName();
    ns.init_search();
    // ns.init_pull();
}
